package FrontEnd;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.data.Range;
import org.jfree.data.category.CategoryDataset;

import BackEnd.ServerInterface;
import Entities.CAuftrag;
import Entities.CHillEstimator;
import FrontEnd.Helper.ServerConnector;
import FrontEnd.Tables.DetailTableModel;
import FrontEnd.Tables.HillTabelModel;

public class HillDialog extends JDialog implements ItemListener {

	private CAuftrag auftrag;
	private Graph graph;
	private GUI gui;
	private JComboBox box;
	private JTable table;
	private HillTabelModel dtm;
	//JScrollPane scrollPane = new JScrollPane();
	private JScrollPane scrollPane;
	JPanel graphPanel = new JPanel();
	
	ChartPanel gpan;
	JFreeChart graphChart;
	CategoryPlot plot;
	ArrayList<CHillEstimator> hill;

	public HillDialog(GUI gui, CAuftrag auftrag, ArrayList<CHillEstimator> hill){
		super(gui, "Hill- Estimator zu " + auftrag.getName(), true);
		this.auftrag=auftrag;
		this.gui = gui;
		this.hill=hill;
		String[] boxStrings = { "Graph", "Tabelle"};
		box = new JComboBox(boxStrings);
		box.addItemListener(this);
		
		Vector<String> columnNames = new Vector<String>();
		columnNames.add("N");
		columnNames.add("Rendite");
		columnNames.add("Hill-Estimator");
		dtm= new HillTabelModel(columnNames,hill);
		table = new JTable(dtm);
		this.scrollPane = new JScrollPane(table);
		
		
		Container cb = getContentPane();
		cb.setLayout(new BorderLayout());
		cb.setBackground(Color.WHITE);
		try {
			cb.add("North", createControlPanel());
			cb.add("Center", this.createGraphPanel());
		} catch (RemoteException e) {
			e.printStackTrace();
		}

		graph = new Graph();
		createCharts();
		
		try {
			drawGraph(hill);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		this.setSize(490, 500);
		this.setLocationRelativeTo(gui);
		this.setResizable(false);
		this.setVisible(true);
	}

	private JPanel createControlPanel(){
		JPanel panel = new JPanel();
		panel.add(box);
		
		return panel;
	}
	
	@Override
	public void itemStateChanged(ItemEvent arg0) {
		aktualisieren();
		
	}
	public void aktualisieren(){
		if(box.getSelectedItem().equals("Graph")){
			removeTable();
			showGraph();
		}
		else{
			removeGraph();
			showTable();
		}
			
	}
	
	public void showGraph(){
		
		this.getContentPane().add("Center", this.graphPanel);
		try {
			drawGraph(hill);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	public void showTable(){
		this.getContentPane().add("Center",scrollPane);
	}

	public void removeTable(){
		this.getContentPane().remove(scrollPane);
		this.getContentPane().validate();
		this.getContentPane().update(this.getContentPane().getGraphics());
	}
	
	public void removeGraph(){
		this.getContentPane().remove(graphPanel);
		this.getContentPane().validate();
		this.getContentPane().update(this.getContentPane().getGraphics());
	}

	public void createCharts(){

		try {
			graphChart = graph.createChartHill(null);
		} catch (RemoteException e) {
			e.printStackTrace();
		}

		//graph.updateChart(graph.createDataset());
		plot = (CategoryPlot) graphChart.getPlot();
		gpan = new ChartPanel(graphChart);
		gpan.setPreferredSize(new Dimension(400,320));
		graphPanel.add(gpan);
		graphPanel.setVisible(true);
	}
	
	private JPanel createGraphPanel() throws RemoteException{
		JLabel ueberschrift = new JLabel("Graphendarstellung");
		ueberschrift.setBounds(10, 10, 100, 20);
		graphPanel.add(ueberschrift);		
		return graphPanel;
	}
	
	public void drawGraph(ArrayList<CHillEstimator> hill) throws RemoteException{
		//bild.setVisible(false);
		if(auftrag != null){
		CategoryDataset dataset = graph.createDatasetHill(hill);
		final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		graph.setAuftrag(auftrag);
		Range range = getRangeHill(hill);
		rangeAxis.setRange(range);
		plot.setDataset(dataset);	
		}
	}
	
	public Range getRangeHill(ArrayList<CHillEstimator> hill) throws RemoteException{
		double maxHill = 0;
		double minHill = 0 ;
		
		for(int i=0;i<hill.size();i++){
			if(maxHill < hill.get(i).getHillEsti()){
				minHill = maxHill;
				maxHill = hill.get(i).getHillEsti();
			}
		}
		Range range = new Range(minHill, maxHill);
		return range;
	}
}
